class Solution(object):
    def maxNumberOfFamilies(self, n, reservedSeats):
        reservedSeats.sort()
        m = len(reservedSeats)
        left = 0
        right = 0
        now = [0] * 10
        zhanju = 1
        ans = 0
        while right < m:
            if reservedSeats[right][0] == reservedSeats[left][0]:
                now[reservedSeats[right][1] - 1] = 1
            else:
                if sum(now[1:5]) == 0:
                    ans += 1
                    if sum(now[5: 9]) == 0:
                        ans += 1
                else:
                    if sum(now[5: 9]) == 0 or sum(now[3: 7]) == 0:
                        ans += 1
                zhanju += 1
                left = right
                now = [0] * 10
                now[reservedSeats[right][1] - 1] = 1
            right += 1
        if sum(now[1:5]) == 0:
            ans += 1
            if sum(now[5: 9]) == 0:
                ans += 1
        else:
            if sum(now[5: 9]) == 0 or sum(now[3: 7]) == 0:
                ans += 1
        return ans + 2 * (n - zhanju)


data = Solution()
n = 3
re = [[1, 2], [1, 3], [1, 8], [2, 6], [3, 1], [3, 10]]
print(data.maxNumberOfFamilies(n, re))
